<?php
class p_chapter extends Model
{
    public $id;
	public $programme_id;
	public $episode_id;
	public $onair_episode_no;
	public $video_id;
	public $in_time;
	public $out_time;
	public $primary_category_id;
	public $created_at;
	public $modified_at;
	public $tags;
	public $ii_data;
	public $title;
	public $title_zh;
	public $title_en;
	public $description;
	public $description_zh;
	public $description_en;
	public $casts;
	public $categories;
	public $infos;
	public $video_type;
	public $premium_title;
	public $premium_title_zh;
	public $premium_title_en;
	public $ppv_flag;
	public $ppv_online_time;
	public $ppv_offline_time;
	public $ppv_country_allow_list;
	public $ppv_country_deny_list;
	public $subscription_flag;
	public $subscription_free_flag;
	public $subscription_online_time;
	public $subscription_offline_time;
	public $subscription_country_allow_list;
	public $subscription_country_deny_list;

    public static function getTableName()
    {
        return 'p_chapter';
    }

    public function set($obj)
    {
        foreach (get_object_vars($this) as $field=>$value) {
            $this->$field = isset($obj->$field) ? $obj->$field : '';
        }
    }

    public function insert($obj)
    {
        return Model::$dbo['w']->save($obj);
    }

    public function update($obj)
    {
        return Model::$dbo['w']->save($obj);
    }

    public function delete($obj)
    {
        return Model::$dbo['w']->deleteInstance($this->getTableName(),$obj->id);
    }

    public function findChapterNotInId(array $id=array(), $programme_id)
    {
        return Model::$dbo['r']->find(__CLASS__, "programme_id=? AND id NOT IN(" . implode(',', $id) . ")", array($programme_id));
    }

    public function findAllByProgrammeId($programme_id)
    {
        return self::$dbo['r']->find(__CLASS__, 'programme_id = ?', array($programme_id));
    }

	/*
	 * by donald
	 * at 2011-10-10
	 */
	public static function getAll(array $params)
	{
		foreach ($params as $field => $value) {
			switch ($field) {
				case 'video_id':
					$where[] = 'video_id = ?';
					$where_array[] = $value;
					break;
			}
		}

		return self::$dbo['w']->find(__CLASS__, implode(' AND ', $where), $where_array);
	}
}
?>
